/* $Id: ProgramDetailsActivity.java 89 2008-01-22 02:38:52Z osborn.steven $
 * 
 * Undroid Package Manager
 *
 * Copyright 2007 Steven Osborn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.bitsetters.undroid;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;

import android.app.Activity;
import android.net.ContentURI;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bitsetters.undroid.common.CommonMenu;
import com.bitsetters.undroid.common.Footer;
import com.bitsetters.undroid.helpers.DbHelper;

/**
 * 
 * @author Steven Osborn - http://steven.bitsetters.com
 */
public class ProgramDetailsActivity extends Activity {

    private static final String TAG = "UNDROID[ProgramDetailsActivity]";
    private DbHelper mDb;
    private TextView app_name;
    private TextView summary;
    private TextView size;
    private TextView author;
    private TextView website;
    private TextView license;
    private TextView details;
    private ImageView icon;
    private Button install_button;

    private TextView size_label;
    private TextView website_label;
    private TextView author_label;
    private TextView license_label;
    private TextView details_label;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.details);

        mDb = new DbHelper(this);

        app_name = (TextView) findViewById(R.id.program_name);
        summary = (TextView) findViewById(R.id.program_summary);
        details = (TextView) findViewById(R.id.program_details);
        size = (TextView) findViewById(R.id.program_size);
        website = (TextView) findViewById(R.id.program_website);
        author = (TextView) findViewById(R.id.program_author);
        license = (TextView) findViewById(R.id.program_license);
        icon = (ImageView) findViewById(R.id.details_icon);
        install_button = (Button) findViewById(R.id.install_button);
        
        size_label = (TextView) findViewById(R.id.program_size_label);
        website_label = (TextView) findViewById(R.id.program_website_label);
        author_label = (TextView) findViewById(R.id.program_author_label);
        license_label = (TextView) findViewById(R.id.program_license_label);
        details_label = (TextView) findViewById(R.id.program_details_label);

        icon.setImageDrawable(getResources().getDrawable(R.drawable.installer64));

        Bundle extras = getIntent().getExtras();            
        int app_id = extras != null ? extras.getInteger(
                ProgramListActivity.APP_ID) : null;


                AppEntry app = mDb.getApp(app_id);

                summary.setText(app.summary);
                details.setText(app.description);
                app_name.setText(app.label);
                
                size.setText(humanizeFileSize(app.size));
                author.setText(app.author);
                website.setText(app.website);
                license.setText(app.license);
                
                // padding left,top,right,bottom
                size_label.setPadding(10, 0, 0, 0);
                website_label.setPadding(10, 0, 0, 0);
                author_label.setPadding(10, 0, 0, 0);
                license_label.setPadding(10, 0, 0, 0);
                details_label.setPadding(10, 0, 0, 0);
                
                app_name.setPadding(0, 10, 0, 0);
                details.setPadding(10, 0, 0, 0);
                size.setPadding(5, 10, 5, 0);
                author.setPadding(5, 0, 5, 0);
                website.setPadding(5, 0, 5, 0);
                license.setPadding(5, 0, 5, 10);

                app_name.setTextSize(20);

                Footer.setup(this);

                install_button.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View arg0) {
                        FileOutputStream out;
                        URL u;
                        BufferedReader in;


                        try {
                            out = openFileOutput("Note_Pad.apk",0);
                            u = new URL("http://undroid.net/repo/packages/Note_Pad.apk");

                            in = new BufferedReader(
                                    new InputStreamReader(u.openStream()) );

                            int buf = 0; 
                            while ((buf = in.read()) >= 0) {
                                out.write((char) buf);
                            }
                            out.flush();
                            out.close();
                            in.close();

                            getPackageManager().installPackage(
                                    new ContentURI("file:///data/data/com.bitsetters.undroid/files/Note_Pad.apk"));
                        } catch (URISyntaxException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            Log.d(TAG,"Oops");
                        }
                    }

                });
    }

    /**
     * 
     */
    protected String humanizeFileSize(int bytes) {
        String file_size;
        double file_size_k;
        double file_size_m;
        
        file_size_k = bytes / 1024;
        file_size_m = file_size_k / 1024;

        if(Math.round(file_size_m) > 0) {
            file_size = String.valueOf(file_size_m) + "m";
        } else if(Math.round(file_size_k) > 0) {
            file_size = String.valueOf(file_size_k) + "k";
        } else {
          file_size = String.valueOf(bytes) + "b";  
        }
        
        return file_size;
    }
    
    /**
     * 
     */
    @Override
    protected void onPause() {
        super.onPause();
        mDb.close();
        mDb = null;
    }

    /**
     * 
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (mDb == null) {
            mDb = new DbHelper(this);
        }
    }

    /**
     * 
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return CommonMenu.createMenu(this, menu);
    }

    /**
     * 
     */
    @Override
    public boolean onOptionsItemSelected(Menu.Item item) {
        return CommonMenu.handleOption(item);
    }

}
